//
//  XYCallingBottomToolBarView.m
//  xylink_app
//
//  Created by gao chenyang on 2020/6/18.
//  Copyright © 2020 ainemo. All rights reserved.
//

#import "XYCallingBottomToolBarView.h"
#import "XYBaseButton.h"
#import "Masonry.h"

static NSString * const KNormalTitle = @"normalTitle";
static NSString * const KSelectTitle = @"selectTitle";
static NSString * const KNormalImgName = @"normalImageName";
static NSString * const KSelectImgName = @"selectImageName";
static NSString * const KItemType = @"itemType";
@interface XYCallingBottomToolBarView()
@property (nonatomic, assign) BOOL muteIsDisabled;
@property (weak, nonatomic) IBOutlet UIView *backView;
@property (weak, nonatomic) IBOutlet UIView *containerView;
@property (nonatomic, strong) NSMutableArray * btnArray;
@property (nonatomic, strong) NSMutableArray * dataArray;
@property (nonatomic, strong) UILabel * participantsNumLabel;
@property (nonatomic, strong) UILabel * messageNumLabel;

@end

@implementation XYCallingBottomToolBarView
+(instancetype)createView{
    XYCallingBottomToolBarView * view = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self.class) owner:self options:nil].firstObject;
    [view _setupDefault];
    [view _setupSubViews];
    return view;
}
-(void)layoutSubviews{
    [super layoutSubviews];
    NSInteger count = self.btnArray.count;
    CGFloat totalW = self.containerView.xy_width;
    CGFloat itemW = 65;
    CGFloat space = (totalW - itemW * count) / (count - 1);
    for (int i = 0; i < count; i++) {
        XYBaseButton * btn = self.btnArray[i];
        btn.xy_x = i * (space + itemW);
        if (btn.tag == XYBottomToolBarItemTypeMore) {
             [self.messageNumLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
                    make.left.equalTo(btn.mas_right).offset(-18.f);
                    make.top.equalTo(btn).offset(0.f);
                    make.width.mas_greaterThanOrEqualTo(18.f);
                    make.height.mas_equalTo(18.f);
            }];
        }
        if (btn.tag == XYBottomToolBarItemTypeHost) {
            [self.participantsNumLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(btn.mas_right).offset(-18.f);
                make.top.equalTo(btn).offset(3.f);
                make.width.mas_equalTo(30.f);
                make.height.mas_equalTo(9.f);
            }];
        }
    }
}
#pragma mark- setup
-(void)_setupDefault{
    self.dataArray = [NSMutableArray arrayWithArray:@[
        @{KNormalTitle : @"关闭视频",KSelectTitle : @"开启视频",KNormalImgName : @"cameraoff_intalking",KSelectImgName : @"cameraon_intalking",KItemType : @(XYBottomToolBarItemTypeVideoMute)},
        @{KNormalTitle : @"静音",KSelectTitle : @"静音",KNormalImgName : @"mutemic_intalking",KSelectImgName : @"unMutemic_intalking",KItemType : @(XYBottomToolBarItemTypeAudioMute)},
        @{KNormalTitle : @"共享",KSelectTitle : @"共享",KNormalImgName : @"share_intalking",KSelectImgName : @"share_intalking",KItemType : @(XYBottomToolBarItemTypeShare)},
        @{KNormalTitle : @"主持会议",KSelectTitle : @"主持会议",KNormalImgName : @"host_intalking",KSelectImgName : @"host_intalking",KItemType : @(XYBottomToolBarItemTypeHost)},
        @{KNormalTitle : @"更多",KSelectTitle : @"更多",KNormalImgName : @"more_intalking",KSelectImgName : @"more_intalking",KItemType : @(XYBottomToolBarItemTypeMore)},
    ]];
}

-(void)_setupSubViews{
    for (NSDictionary * dic in self.dataArray) {
        XYBaseButtonConfig * config = [XYBaseButtonConfig buttonConfig];
        config.styleType = XYButtonStyleTypeTop;
        config.titleFont = [UIFont systemFontOfSize:10];
        config.title = dic[KNormalTitle];
        config.selectTitle = dic[KSelectTitle];
        config.titleSize = CGSizeMake(65, 14);
        config.titleOffsetTop = 28;

        config.normalImage = [UIImage imageNamed:dic[KNormalImgName]];
        config.selectImage = [UIImage imageNamed:dic[KSelectImgName]];
        XYBaseButton * btn = [XYBaseButton buttonFrame:CGRectMake(0, 8, 65, 44) ButtonConfig:config];
        btn.tag = [dic[KItemType] integerValue];
        if (btn.tag == XYBottomToolBarItemTypeAudioMute) {
            UILongPressGestureRecognizer * longGes = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressEvent)];
            longGes.minimumPressDuration = 5;
            [btn addGestureRecognizer:longGes];
        }
        [btn addTarget:self action:@selector(clickEvent:) forControlEvents:UIControlEventTouchUpInside];
        [self.containerView addSubview:btn];
        [self.btnArray addObject:btn];
    }
    [self.containerView addSubview:self.participantsNumLabel];
    [self.containerView addSubview:self.messageNumLabel];
}
#pragma mark- public methods

-(void)updateItemWithTitle:(nullable NSString *)title forState:(UIControlState)state itemType:(XYBottomToolBarItemType)type{
    if (title) {
        XYBaseButton * btn = (XYBaseButton *)[self.containerView viewWithTag:type];
        if (btn) {
            [btn setTitle:title forState:state];
        }
    }
}

-(void)updateItemWithImage:(nullable UIImage *)image forState:(UIControlState)state itemType:(XYBottomToolBarItemType)type{
    if (image) {
        XYBaseButton * btn = (XYBaseButton *)[self.containerView viewWithTag:type];
        if (btn) {
            [btn setImage:image forState:state];
        }
    }
}

-(void)updateItemWithEnableStatus:(BOOL)enable itemType:(XYBottomToolBarItemType)type{
    XYBaseButton * btn = (XYBaseButton *)[self.containerView viewWithTag:type];
    if (btn) {
        btn.enabled = enable;
    }
}

-(void)updateItemWithSelectedStatus:(BOOL)selected itemType:(XYBottomToolBarItemType)type{
    XYBaseButton * btn = (XYBaseButton *)[self.containerView viewWithTag:type];
    if (btn) {
        btn.selected = selected;
    }
}

-(BOOL)getItemSelectedStatusWithType:(XYBottomToolBarItemType)type{
    XYBaseButton * btn = (XYBaseButton *)[self.containerView viewWithTag:type];
    if (btn) {
        return btn.selected;
    }
    return NO;
}

-(void)updateParticipantsNum:(NSInteger)num{
    if (num > 998) {
        self.participantsNumLabel.text = @"999+";
    } else {
        self.participantsNumLabel.text = [NSString stringWithFormat:@"%zd", num];
    }
}

-(void)updateMessageNum:(NSInteger)num{
    dispatch_async(dispatch_get_main_queue(), ^{
        if (num > 0) {
            self.messageNumLabel.hidden = NO;
            if (num > 99) {
                self.messageNumLabel.text = @"···";
            }else{
                self.messageNumLabel.text = [NSString stringWithFormat:@"%zd",num];
            }
        }else{
            self.messageNumLabel.hidden = YES;
            self.messageNumLabel.text = @"";
        }
    });
}

-(void)updateShareItemStatus:(BOOL)share{
    if (share) {
        [self updateItemWithTitle:@"结束共享" forState:UIControlStateNormal itemType:XYBottomToolBarItemTypeShare];
        [self updateItemWithImage:[UIImage imageNamed:@"end_shareing"] forState:UIControlStateNormal itemType:XYBottomToolBarItemTypeShare];
    }else{
        [self updateItemWithTitle:@"共享" forState:UIControlStateNormal itemType:XYBottomToolBarItemTypeShare];
        [self updateItemWithImage:[UIImage imageNamed:@"share_intalking"] forState:UIControlStateNormal itemType:XYBottomToolBarItemTypeShare];
    }
}

-(void)updataAudioStatusWithMuteIsDisabled:(BOOL)muteIsDisabled handStatus:(BOOL)handStatus{
    self.muteIsDisabled = muteIsDisabled;
    if (muteIsDisabled){
        //有申请发言
        [self updateItemWithTitle:@"结束发言" forState:UIControlStateNormal itemType:XYBottomToolBarItemTypeAudioMute];
        [self updateItemWithImage:[UIImage imageNamed:@"end_speech"] forState:UIControlStateNormal itemType:XYBottomToolBarItemTypeAudioMute];

        if (!handStatus){
            //没有举手 显示举手图标
            [self updateItemWithTitle:@"举手发言" forState:UIControlStateSelected itemType:XYBottomToolBarItemTypeAudioMute];
            [self updateItemWithImage:[UIImage imageNamed:@"hands_up"] forState:UIControlStateSelected itemType:XYBottomToolBarItemTypeAudioMute];
        }else{
            //已举手 显示取消举手图标
            [self updateItemWithTitle:@"取消举手" forState:UIControlStateSelected itemType:XYBottomToolBarItemTypeAudioMute];
            [self updateItemWithImage:[UIImage imageNamed:@"hands_cancel"] forState:UIControlStateSelected itemType:XYBottomToolBarItemTypeAudioMute];
        }
    }else{
        //正常audio 按钮显示状态
        [self updateItemWithTitle:@"静音" forState:UIControlStateNormal itemType:XYBottomToolBarItemTypeAudioMute];
        [self updateItemWithImage:[UIImage imageNamed:@"mutemic_intalking"] forState:UIControlStateNormal itemType:XYBottomToolBarItemTypeAudioMute];
        
        [self updateItemWithTitle:@"静音" forState:UIControlStateSelected itemType:XYBottomToolBarItemTypeAudioMute];
        [self updateItemWithImage:[UIImage imageNamed:@"unMutemic_intalking"] forState:UIControlStateSelected itemType:XYBottomToolBarItemTypeAudioMute];
    }
}

- (CGFloat)left {
    return self.containerView.xy_x;
}

#pragma mark- actions
-(void)clickEvent:(XYBaseButton *)btn{
    if (self.clickEventBlock) {
        self.clickEventBlock(btn);
    }
}
-(void)longPressEvent{
    if (self.longPressEventBlock) {
        self.longPressEventBlock();
    }
}
-(void)microphoneViewTaped:(UITapGestureRecognizer *)tap{
    XYBaseButton * btn = (XYBaseButton *)[self.containerView viewWithTag:XYBottomToolBarItemTypeAudioMute];
    if (btn) {
        [self clickEvent:btn];
    }
}
#pragma mark- lazy
-(NSMutableArray *)btnArray{
    if (!_btnArray) {
        _btnArray = [NSMutableArray arrayWithCapacity:0];
    }
    return _btnArray;
}

- (UILabel *)participantsNumLabel {
    if (!_participantsNumLabel) {
        _participantsNumLabel = [[UILabel alloc] init];
        _participantsNumLabel.font = [UIFont systemFontOfSize:9.f];
        _participantsNumLabel.textColor = [UIColor colorWithWhite:1 alpha:0.9];
    }
    return _participantsNumLabel;
}

@end
